<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link href="css/mui.css" rel="stylesheet" />
		<link href="css/bitCoin.css" rel="stylesheet" />
	</head>

	<body>


		<div id="app">


			<header class="mui-bar mui-bar-nav">
				<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"><span class="mui-badge mui-btn-red"
						v-show="userInfo.health<80 ">hp({{userInfo.health}})</span></a>
				<h1 class="mui-title">虚拟货币行情({{userInfo.days | day2DateFilter}})</h1>
			</header>


			<div class="mui-content">


				<div class="mui-content-padded">
					<div class="chart" id="lineChart"></div>
				</div>

				<ul class="mui-table-view">
					<li v-for="item in hangqingArr" @click="readHangqing(item)" class="mui-table-view-cell mui-media">
						<a href="javascript:;">
							<img class="mui-media-object mui-pull-left" :src="item.pic">
							<div class="mui-media-body">
								{{item.title}}
								<p class="mui-ellipsis">
									{{item.content}}
								</p>
							</div>
						</a>
					</li>

				</ul>

				<hr />
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						买入均价:{{userInfo.bitCoinBuyAvg}}
					</li>
					<li class="mui-table-view-cell">
						持币数量:{{userInfo.bitCoinCount}}
					</li>
					<li class="mui-table-view-cell">
						货币盈亏:{{userInfo.bitCoinProfit | profitFilter}}
					</li>
				</ul>

			</div>
			<!-- <button type="button" class="mui-btn mui-btn-blue" @click="test()">按钮</button> -->
			<!-- <div class="mui-content-padded">
				<Span>买入价格:111</Span>
				<Span>持币数量:222</Span>
				<Span>货币盈亏:333</Span>
			</div> -->


			<nav class="mui-bar mui-bar-tab">
				<a class="mui-tab-item2" @click="buyCoin()">
					<!-- <span class="mui-icon mui-icon-home"></span> -->
					<span class="mui-tab-label">买入货币</span>
				</a>
				<a class="mui-tab-item2" @click="sellCoin()">
					<!-- <span class="mui-icon mui-icon-phone"></span> -->
					<span class="mui-tab-label">卖出货币</span>
				</a>
				<a class="mui-tab-item2" @click="showTradeLog()">
					<!-- <span class="mui-icon mui-icon-email"></span> -->
					<span class="mui-tab-label">交易记录</span>
				</a>
				<a class="mui-tab-item2" @click="nextDay()">
					<!-- <span class="mui-icon mui-icon-gear"></span> -->
					<span class="mui-tab-label">下一天</span>
				</a>
			</nav>

		</div>












		<script src="js/echarts-all.js"></script>
		<script src="js/util.js"></script>
		<script src="js/db.js"></script>
		<script src="js/mui.js"></script>
		<script src="js/vue-min.js"></script>
		<script type="text/javascript">
			mui.init()




			var app = new Vue({
				el: '#app',
				data: {
					userInfo: {
						niumaLevel: 1,
						name: '狗蛋',
						age: 0,
						IQ: 10,
						luck: 100,
						// money: 0,
						energyMax: 100,
						energyNow: 100,
						parentMoney: 100000,
						health: 100,
						hunger: 50,
						clean: 50,
						happy: 50,
						days: 1,
						healthLogs: [],
						bitCoinCount: 0,
						bitCoinTradeLog: [],
						bitCoinBuyAvg: 0,
						bitCoinProfit: 0,
						changeArr: [986.07, 29.67, 1899.80, 845.65, 301.77, 940.28, 213.06, 1824.48, 1913.19, 1000.02,
							1401.34, 1847.38,
							986.07, 29.67, 1899.80, 845.65, 301.77, 940.28, 63.72, 1877.40,
							1115.50, 657.85, 1279.25, 80.96, 1381.79, 907.53, 1459.61, 1200.13
						],
						todayWorkCount: 0,
						appearance: 0,
						clothingList: [],
						reputation: 0,
						houseList: [],
						baomuCareDay: 0
					},
					changeArr: [986.07, 29.67, 1899.80, 845.65, 301.77, 940.28, 213.06, 1824.48, 1913.19, 1000.02,
						1401.34, 1847.38,
						986.07, 29.67, 1899.80, 845.65, 301.77, 940.28, 63.72, 1877.40,
						1115.50, 657.85, 1279.25, 80.96, 1381.79, 907.53, 1459.61, 1200.13
					],
					countryArr: ["中国", "美国", "印度", "巴西", "德国", "日本", "加拿大", "俄罗斯", "法国", "英国"],
					bankArr: ["摩根大通银行", "美国银行", "花旗银行", "富国银行", "PNC银行", "美国合众银行", "德意志银行", "TD银行", "巴克莱银行", "大通银行"],
					dazhouArr: ["亚洲", "欧洲", "北美洲", "南美洲", "非洲", "大洋洲", "南极洲", "北极洲", "加勒比海地区", "中东地区"],
					hangqingArr: [{
						pic: 'pic/bit-pic-1.png',
						title: '比特币价格近期行情分析',
						content: '近期，比特币在最近一周持续xx，最高达到了xxxxxx元，随后稍微回落，目前价格在xxxxx元左右。这一涨幅使得比特币在月内增长了近xxxx%',
						change: 0.1,
						id: 1
					}, {
						pic: 'pic/bit-pic-2.png',
						title: '减半事件的市场反应',
						content: '比特币的第四次“减半”发生在2024年4月，矿工的奖励从6.25比特币减少到3.125比特币。虽然历史数据显示减半后通常会对价格产生正面影响，但近期机构分析师对比特币的看法较为悲观，认为此次减半后价格可能不会上涨，甚至可能出现下跌。分析师指出，这种看法主要基于市场对减半的预期已经反映在当前价格中',
						change: -0.1,
						id: 2
					}, {
						pic: 'pic/bit-pic-3.png',
						title: '政府出售被没收的比特币',
						content: '美国政府计划出售约69000个从“丝绸之路”市场没收的比特币，这一举动可能会对市场造成下行压力。近期美国最高法院拒绝了对此案的上诉，进一步推动了这一出售的可能性。分析师警告，这可能会引发投资者对市场的担忧，从而影响比特币的价格',
						change: -0.1,
						id: 3
					}, {
						pic: 'pic/bit-pic-1.png',
						title: '美联储可能的降息预期',
						content: '最近比特币价格上涨主要是由于市场对美联储可能降息的预期。比特币在某个交易日内涨幅达到6.4%，使价格回升至61,337美元。这种上涨被认为是投资者对未来风险资产的乐观预期，特别是在美联储宣布即将降息的背景下',
						change: 0.1,
						id: 4
					}, {
						pic: 'pic/bit-pic-2.png',
						title: '中国经济刺激措施',
						content: '中国政府最近承诺将“大幅增加”债务以支持经济增长，尽管未透露具体刺激措施的规模。此消息引发市场对经济复苏的期待，同时也推动了比特币的价格上涨。这表明投资者正在寻找避险资产，期望从刺激措施中受益',
						change: 0.1,
						id: 5
					}, {
						pic: 'pic/bit-pic-3.png',
						title: '全球加密货币市场的变化',
						content: '由于对美联储政策的乐观预期以及中国的经济刺激措施，全球加密货币市场显示出复苏迹象。近期数据表明，加密货币基金的投资增加，表明投资者对比特币的兴趣回升，这可能会推动价格进一步上涨',
						change: 0.1,
						id: 6
					}],
					priceChangeFactor: 1

				},
				methods: {
					nextDay() {
						if (this.userInfo.clean >= 100 && this.userInfo.happy >= 100 && this.userInfo.hunger <= 30) {
							this.userInfo.health += 10
						}
						if (this.userInfo.baomuCareDay == 0) {
							this.userInfo.hunger += this.userInfo.hunger < 10 ? 90 : (100 - this.userInfo.hunger)
						}


						this.userInfo.health -= Math.floor((this.userInfo.hunger) / 15)
						this.userInfo.health -= Math.floor((100 - this.userInfo.clean) / 15)
						this.userInfo.health -= Math.floor((100 - this.userInfo.happy) / 15)

						this.userInfo.luck += Math.random() * 101 > this.userInfo.luck ? 1 : -1

						this.userInfo.health -= Math.floor((80 - this.userInfo.luck) / 15)

						if (this.userInfo.clean >= 70 && this.userInfo.baomuCareDay == 0) {
							this.userInfo.clean -= 70
						} else {
							if (this.userInfo.baomuCareDay == 0) {
								this.userInfo.clean = 0
							}

						}

						let happyHealthFactor = 1
						if (this.userInfo.happy < 50 && this.userInfo.baomuCareDay == 0) {
							this.userInfo.happy = this.userInfo.happy < 0 ? -this.userInfo.happy : this.userInfo.happy
							happyHealthFactor += Math.floor(this.userInfo.happy / 10)
						}

						this.userInfo.energyNow = this.userInfo.energyMax



						this.userInfo.days += 1
						//特殊节日处理 孩子开心增加
						let date = convertDaysToYearsMonthsDays(this.userInfo.days)
						if (date.years == 0 && date.months == 1 && date.days == 0) {
							this.userInfo.parentMoney += ((4 - this.userInfo.niumaLevel) * 5000)
							mui.alert('孩子满月 收到' + (4 - this.userInfo.niumaLevel) * 5000 + '分子钱', '生日快乐')
						}
						//幼儿园学费  不上幼儿园 每年智力减少 
						if (date.years >= 3 && date.years <= 5 && date.months == 0 && date.days == 0) {
							this.userInfo.parentMoney -= 5000
							mui.alert('交幼儿园学费 花费' + 5000 + '元', '新学期开始咯')
						}

						if (this.userInfo.days % 365 == 0) {
							this.userInfo.IQ += parseInt((7 * calIntAvgByArr(this.userInfo.healthLogs) / 100))
						}

						this.userInfo.health = this.userInfo.health > 100 ? 100 : this.userInfo.health

						this.userInfo.healthLogs.push(parseInt(this.userInfo.health))



						Database.saveData('userInfo', JSON.stringify(this.userInfo))
						this.randomCoinEvent()
						this.userInfo.bitCoinBuyAvg = this.calBuyAvg()
						this.userInfo.bitCoinProfit = parseFloat(this.calProfit().toFixed(2))

						for (var i = 0; i < this.userInfo.clothingList.length; i++) {
							if (this.userInfo.clothingList[i].expire > 0) {
								this.userInfo.clothingList[i].expire -= 1
							}
						}

						if (this.userInfo.baomuCareDay > 0) {
							this.userInfo.baomuCareDay -= 1
						}

						Database.saveData('userInfo', JSON.stringify(this.userInfo))
						mui.toast('又过了一天')
						if (this.userInfo.health <= 0) {
							Database.deleteData('userInfo')
							mui.alert('你娃挂了', function() {
								document.location.href = 'birth.html'
							})

							return
						}
					},
					readHangqing(item) {
						switch (item.id) {
							case 1:
								item.content = '近期，比特币在最近一周行情：</br>'
								for (var i = this.userInfo.changeArr.length - 7; i < this.userInfo.changeArr
									.length; i++) {
									item.content += (parseInt(this.userInfo.changeArr[i]) + '元 ')
								}
								let now = parseInt((this.userInfo.changeArr[this.userInfo.changeArr.length - 1]).toFixed(
									2))
								let qian7 = parseInt((this.userInfo.changeArr[this.userInfo.changeArr.length - 7])
									.toFixed(2))
								let updownText = now > qian7 ? '涨幅' : '跌幅'
								let updownText2 = now > qian7 ? '增长' : '下滑'
								item.content += ('这一' + updownText + '涨幅使得比特币在月内' + updownText2 + '了近')
								let change = now - qian7
								let fudu = 0
								if (change > 0) {
									fudu = parseInt(change * 100 / qian7)
								} else {
									fudu = -parseInt(change * 100 / qian7)
								}
								item.content += (fudu + '%')
								break;
							case 2:

								break;
							case 3:
								break;
							case 4:
								break;
							case 5:
								break;
							case 6:
								break;
							default:
								break;
						}

						mui.alert(item.content, item.title)
					},
					hangqingProcess() {


					},
					initChart() {
						// let that = this
						let avg = (this.changeArr.reduce((total, num) => total + num, 0) / this.changeArr.length)
							.toFixed(2);
						let max = Math.max.apply(null, this.changeArr).toFixed(2);
						let now = (this.changeArr[this.changeArr.length - 1]).toFixed(2)
						console.log('now:' + now)
						let option = {
							legend: {
								data: ['实价:' + now, '最高:' + max, '平均:' + avg],
								// borderColor:'#ccc',
								textStyle: {
									color: 'auto'
								}
							},
							grid: {
								x: 35,
								x2: 10,
								y: 30,
								y2: 25
							},
							toolbox: {
								show: false,
								feature: {
									mark: {
										show: true
									},
									dataView: {
										show: true,
										readOnly: false
									},
									magicType: {
										show: true,
										type: ['line', 'bar']
									},
									restore: {
										show: true
									},
									saveAsImage: {
										show: true
									}
								}
							},
							calculable: false,
							xAxis: [{
								type: 'category',
								data: ['', '', '', '', '', '', '', '', '', '', '', '',
									'', '', '', '', '', '', '', '', '', '', '', ''
								]
							}],
							yAxis: [{
								type: 'value',
								splitArea: {
									show: true
								}
							}],
							series: [{
								name: '',
								type: 'line',
								data: []
							}]
						}
						option.series[0].data = this.changeArr

						var byId = function(id) {
							return document.getElementById(id);
						};
						var lineChart = echarts.init(byId('lineChart'));
						lineChart.setOption(option);
						console.log('实时：' + this.changeArr[this.changeArr.length - 1])
						console.log(this.changeArr)
					},
					coinPriceChange() {
						// this.randomCoinEvent()
						// console.log(this.changeArr)
						this.changeArr.splice(0, 1)
						// function getRandomNumber(min, max) {
						//   return Math.floor(Math.random() * (max - min + 1)) + min;
						// }
						let change = ((Math.random() * (100 - (-100)) + 1) - 100).toFixed(2)
						let newPrice = this.changeArr[this.changeArr.length - 1] + parseFloat(change)
						console.log(change)
						if (newPrice >= 1) {
							for (var i = 0; i < 1; i++) {
								this.changeArr.push(newPrice)
							}
							// console.log(this.changeArr[this.changeArr.length - 1])
						} else {
							this.changeArr.push(1)
						}
						this.userInfo.changeArr = this.changeArr
						Database.saveData('userInfo', JSON.stringify(this.userInfo))
						// console.log(this.changeArr[this.changeArr.length - 1])
						this.initChart()
					},
					randomCoinEvent() {
						if (Math.random() > 0.9) {

							this.changeArr.splice(0, 1)
							if (this.changeArr[this.changeArr.length - 1] < 10) {
								let change = ((Math.random() * (2500 - (1000)) + 1) + 1000).toFixed(2)
								let newPrice = this.changeArr[this.changeArr.length - 1] + parseFloat(change)
								this.changeArr.push(parseFloat(newPrice))
								mui.alert('行情大涨!')
							} else {


								let change = ((Math.random() * (2000 - (-2000)) + 1) - 2000).toFixed(2)

								if (((change > 0 ? change : -change) / this.changeArr[this.changeArr.length - 1]) >
									0.5 && change < 0) {
									mui.alert('行情暴跌!')
								}

								if (((change > 0 ? change : -change) / this.changeArr[this.changeArr.length - 1]) >
									0.5 && change > 0) {
									mui.alert('小涨一波!')
								}

								let newPrice = this.changeArr[this.changeArr.length - 1] + parseFloat(change)
								if (newPrice >= 1) {
									this.changeArr.push(parseFloat(newPrice))
								} else {
									this.changeArr.push(1)
								}
							}
							this.initChart()

						} else {
							this.coinPriceChange()
						}


					},
					buyCoin() {
						let now = parseFloat((this.changeArr[this.changeArr.length - 1]).toFixed(2))
						let yesterday = parseFloat((this.changeArr[this.changeArr.length - 2]).toFixed(2))
						if (this.userInfo.parentMoney < now) {
							mui.alert('资金不足 无法购买')
						} else {

							if (yesterday > now && ((yesterday - now) / yesterday) > 0.5 || now == 1) {
								mui.alert('货币已跌停 禁止买入')
								return
							}

							let that = this
							let buyMax = Math.floor(this.userInfo.parentMoney / now)
							mui.prompt('当前价格:' + now, '最大可购买数量:' + buyMax, '交易信息', ['确认买入', '取消交易'], function(e) {
								let buyNum = parseInt(e.value)
								if (e.index == 0) {
									// console.log(typeof(e.value)=='number')
									if (buyNum <= buyMax && buyNum >= 1) {
										that.userInfo.parentMoney -= (buyNum * now)
										that.userInfo.bitCoinCount += buyNum
										// if(that.userInfo.bitCoinTradeLog.length==10){
										// 	that.userInfo.bitCoinTradeLog.splice(0,1)
										// }
										that.userInfo.bitCoinTradeLog.push({
											type: 'buy',
											price: now,
											num: buyNum
										})
										that.userInfo.bitCoinBuyAvg = that.calBuyAvg()
										that.userInfo.bitCoinProfit = parseFloat(that.calProfit().toFixed(2))
										that.userInfo.parentMoney -= parseInt((buyNum * now * 0.001).toFixed(2))

										Database.saveData('userInfo', JSON.stringify(that.userInfo))
										mui.toast('交易成功')
									} else {
										// that.userInfo.name = e.value
									}
								} else {
									mui.toast('输入有误', '交易失败')
								}

							}, 'div')
						}
					},
					calBuyAvg() {
						let sum = 0
						let count = 0
						// let avg = 0
						for (var i = 0; i < this.userInfo.bitCoinTradeLog.length; i++) {

							if (this.userInfo.bitCoinTradeLog[i].type == 'buy') {
								sum += (this.userInfo.bitCoinTradeLog[i].price * this.userInfo.bitCoinTradeLog[i].num)
								count += this.userInfo.bitCoinTradeLog[i].num

							}
							if (this.userInfo.bitCoinTradeLog[i].type == 'sell') {
								sum -= (sum * this.userInfo.bitCoinTradeLog[i].num / count)
								count -= this.userInfo.bitCoinTradeLog[i].num
							}
						}
						return parseInt(this.userInfo.bitCoinCount) == 0 ? 0 : parseFloat((sum / count).toFixed(2))
					},
					calProfit() {
						this.calBuyAvg()
						let now = parseFloat((this.changeArr[this.changeArr.length - 1]).toFixed(2))
						return this.userInfo.bitCoinCount * (now - this.calBuyAvg())
					},
					showTradeLog() {
						// type:'buy',
						// price: now,
						// num: buyNum
						let str = ''
						let showLogNum = 10
						let resArr = this.userInfo.bitCoinTradeLog.slice(-showLogNum)
						for (let i = 0; i < resArr.length; i++) {
							str += resArr[i].type == 'buy' ? '买入' : '卖出'
							str += '数量: '
							str += resArr[i].num
							str += ' 交易价格: '
							str += resArr[i].price
							str += '</br>'
						}
						mui.alert(str, '近' + showLogNum + '次交易记录')
					},
					sellCoin() {
						if (this.userInfo.bitCoinCount == 0) {
							mui.toast('持仓为0 交易失败')
						} else {
							let that = this
							let now = parseFloat((this.changeArr[this.changeArr.length - 1]).toFixed(2))
							let yesterday = parseFloat((this.changeArr[this.changeArr.length - 2]).toFixed(2))
							if (yesterday < now && ((now - yesterday) / now) > 0.5) {
								mui.alert('货币已涨停 禁止售出')
								return
							}

							let sellMax = this.userInfo.bitCoinCount
							mui.prompt('当前价格:' + now, '最大可卖出数量:' + sellMax, '交易信息', ['确认卖出', '取消交易'], function(e) {
								let sellNum = parseInt(e.value)
								if (e.index == 0) {
									// console.log(typeof(e.value)=='number')
									if (sellNum <= sellMax && sellNum >= 1) {
										that.userInfo.parentMoney += (sellNum * now)
										that.userInfo.bitCoinCount -= sellNum
										// if(that.userInfo.bitCoinTradeLog.length==10){
										// 	that.userInfo.bitCoinTradeLog.splice(0,1)
										// }
										that.userInfo.bitCoinTradeLog.push({
											type: 'sell',
											price: now,
											num: sellNum
										})
										that.userInfo.bitCoinBuyAvg = that.calBuyAvg()
										that.userInfo.bitCoinProfit = parseFloat(that.calProfit().toFixed(2))
										that.userInfo.parentMoney += parseInt((sellNum * now * 0.001).toFixed(2))

										Database.saveData('userInfo', JSON.stringify(that.userInfo))
										mui.toast('交易成功')
									} else {
										// that.userInfo.name = e.value
									}
								} else {

								}

							}, 'div')
						}
					},
					test() {
						this.randomCoinEvent()

					}
				},
				filters: {
					day2DateFilter(val) {
						// val = 30000
						let res = convertDaysToYearsMonthsDays(val)

						let dateStr = (res.years + '年' + res.months + '月' + res.days + '天')

						return dateStr
					},
					profitFilter(val) {
						return val.toFixed(2)
					}
				},
				mounted() {


					if (Database.getData('userInfo') == null) {
						document.location.href = 'birth.html'
					} else {
						this.userInfo = JSON.parse(Database.getData('userInfo'))

						this.changeArr = this.userInfo.changeArr
						this.initChart()
					}
					// this.userInfo.bitCoinProfit = 0
					let that = this
					// setInterval(function() {
					// 	that.coinPriceChange()
					// 	// console.log(that.changeArr)
					// }, 1000)
					// mui.alert('中国政府最近承诺将“大幅增加”债务以支持经济增长，尽管未透露具体刺激措施的规模。此消息引发市场对经济复苏的期待，同时也推动了比特币的价格上涨。这表明投资者正在寻找避险资产，期望从刺激措施中受益','中国经济刺激措施')
					this.hangqingProcess()

				},
				beforeMount() {

				}
			})
		</script>
	</body>

</html>